package utils

import (
	"go.uber.org/zap"
	"golang.org/x/text/encoding/simplifiedchinese"
	"os"
	"os/exec"
	"strings"
	"time"
	"virus-proxy/internal/logger"
	"virus-proxy/internal/mount"
)

// FormatTime 格式化时间
func FormatTime(t time.Time) string {
	return t.Format("2006-01-02 15:04:05")
}

// FormatDate 格式化日期
func FormatDate(t time.Time) string {
	return t.Format("2006-01-02")
}

// ConvertToScanList 将传入的scanList转换为本机挂载的文件路径 例如: //172.0.2.238/share -> Z:share
func ConvertToScanList(scanList []string, clientIp string) (newScanList []string) {
	for i := range scanList {
		scanList[i] = "//" + clientIp + scanList[i]
		for j := mount.MountNum - 1; j >= mount.MinMountNum; j-- {
			if strings.Contains(scanList[i], mount.MountSliceIns.Drives[j]) && mount.MountSliceIns.Drives[j] != "" {
				path := strings.TrimPrefix(scanList[i], mount.MountSliceIns.Drives[j])
				newScanList = append(newScanList, strings.ReplaceAll(string('A'+rune(j))+":"+path, "/", "\\"))
			}
		}
	}
	return newScanList
}

// ConvertToScanList 将传入的scanList转换为火绒可识别的文件路径 例如: //172.0.2.238/share -> \\?\UNC\172.0.2.238\share
func ConvertToHuorong(scanList []string, clientIp string) []string {
	for i := range scanList {
		scanList[i] = "\\\\" + clientIp + strings.ReplaceAll(scanList[i], "/", "\\")
	}
	return scanList
}

func Whoami() {
	cmd := exec.Command("whoami")
	out, err := cmd.CombinedOutput()
	if err != nil {
		logger.LogDebug("whoami failed", zap.Error(err))
		return
	}
	output, err := simplifiedchinese.GB18030.NewDecoder().String(string(out)) //转换字符集，解决UTF-8乱码
	if err != nil {
		logger.LogDebug("Transcode failed. ", zap.Error(err))
		logger.LogDebug("", zap.String("Whoami", string(out)))
		return
	}
	logger.LogDebug("", zap.String("whoami", string(output)))
}

func EchoPath() {
	cmd := exec.Command("cmd", "/c", "set")
	out, err := cmd.CombinedOutput()
	if err != nil {
		logger.LogDebug("set failed ", zap.Error(err))
		return
	}
	output, err := simplifiedchinese.GB18030.NewDecoder().String(string(out)) //转换字符集，解决UTF-8乱码
	if err != nil {
		logger.LogDebug("Transcode failed. ", zap.Error(err))
		logger.LogDebug("", zap.String("EchoPath", string(out)))
		return
	}
	logger.LogDebug("", zap.String("EchoPath", output))
}

func Cd() {
	//cmd := exec.Command("C:\\WINDOWS\\system32\\cmd.exe", "cd", "D:")
	filePath := "Z:\\a.txt"

	// 读取文件内容
	content, err := os.ReadFile(filePath)
	if err != nil {
		logger.LogDebug("read file failed ", zap.Error(err))
		return
	}
	logger.LogDebug("", zap.String("EchoPath", string(content)))
}
